package com.qmerak.dao.impl;

import java.util.List;
import java.util.UUID;

import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.qmerak.dao.HibernateDao;
import com.qmerak.entity.MemSalvageEntity;
import com.qmerak.entity.MemberEntity;

/**
 * @author Shaohong•Cheng
 * @mail shaohong726@126.com
 * @date 2013-7-2
 * @project TUManager
 */
@Repository
public class MemSalvageDao extends HibernateDao<MemSalvageEntity, String> {
	public MemSalvageEntity add(MemSalvageEntity entity) {
		entity.setId(UUID.randomUUID().toString());
		return super.add(entity);
	}

	@SuppressWarnings("unchecked")
	public List<MemSalvageEntity> findUnapproveSalvages() {
		Session session = this.sessionFactory.openSession();
		List<MemSalvageEntity> list = session
				.createQuery("from MemSalvageEntity where state=?")
				.setParameter(0, MemSalvageEntity.SalvageState.APPLYING.name())
				.list();
		session.close();
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<MemSalvageEntity> findUnfeedbackSalvages(
			MemberEntity memberEntity) {
		Session session = this.sessionFactory.openSession();
		List<MemSalvageEntity> list = session
				.createQuery(
						"from MemSalvageEntity where state=? and memberID=?")
				.setParameter(0,
						MemSalvageEntity.SalvageState.APPROVEDAGREE.name())
				.setParameter(1, memberEntity.getId()).list();
		session.close();
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<MemSalvageEntity> findFeedbackedSalvages(
			MemberEntity memberEntity) {
		Session session = this.sessionFactory.openSession();
		List<MemSalvageEntity> list = session
				.createQuery(
						"from MemSalvageEntity where state=? and feedbackPerson=?")
				.setParameter(0,
						MemSalvageEntity.SalvageState.FEEDBACKED.name())
				.setParameter(1, memberEntity.getId()).list();
		session.close();
		return list;
	}

	public void updateState(MemSalvageEntity salvageEntity,
			MemberEntity operater) {
	}

	@SuppressWarnings("unchecked")
	public List<MemSalvageEntity> findAppliedSalvages(MemberEntity memberEntity) {
		Session session = this.sessionFactory.openSession();
		List<MemSalvageEntity> list = session
				.createQuery("from MemSalvageEntity where localOperator=?")
				.setParameter(0, memberEntity.getId()).list();
		session.close();
		return list;
	}
}
